package algorithm.poj.p3000;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLDecoder;


/**
 * 分析：
 * 实现：
 * 经验：
 * 教训：
 * 
 * @author wong.tong@gmail.com
 *
 */
public class P3751 {

	public static void main(String[] args) throws Exception {

		InputStream input = null;
		if (false) {
			input = System.in;
		} else {
			URL url = P3751.class.getResource("P3751.txt");
			File file = new File(URLDecoder.decode(url.getPath(), "UTF-8"));
			input = new FileInputStream(file);
		}
		
		BufferedReader stdin = new BufferedReader(new InputStreamReader(input));

		String line = stdin.readLine();
		int T = Integer.valueOf(line.trim());
		for (int i = 0; i < T; i ++) {
			line = stdin.readLine();
			String year = line.substring(0, 4);
			String month = line.substring(5, 7);
			String day = line.substring(8, 10);
			String hour = line.substring(11, 13);
			String minute = line.substring(14, 16);
			String second = line.substring(17, 19);
			
			int h = Integer.valueOf(hour);
			String suffix = "am";
			if (h >= 12) {
				suffix = "pm";
			}
			if (h == 0) {
				hour = "12";
			} else if (h > 12) {
				h -= 12;
				hour = String.valueOf(h);
				if (hour.length() < 2) hour = "0" + hour;
			}
			
			System.out.println(month + "/" + day + "/" + year 
						+ "-" + hour + ":" + minute + ":" + second + suffix);
		}
	}
	
}